package com.office.anywher.sehedule;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.office.anywher.beans.UserInfo;
import com.office.anywher.execeptions.OAException;
import com.office.anywher.https.HttpClientService;
import com.office.anywher.poly.list.Common;
import com.wenxy.common.IConst;

public class ScheduleListReceiver extends Common {
	private SimpleDateFormat mSdf = new SimpleDateFormat("yyyyMMdd HH:mm");
	public List<ScheduleInfo> receive(HttpClientService aService, int start,
			int rowNum) throws Exception {
		List<ScheduleInfo> rtnList = new ArrayList<ScheduleInfo>();
		UserInfo aUserInfo = aService.getUserInfo();
		JSONArray aJsonArray = aService.getPlanList(IConst.Http.Plan.URL,
				aUserInfo.getUserOrganId(), start, rowNum);
		if (aJsonArray == null || aJsonArray.length() <= 0) {
			throw new OAException(OAException.OA_EXECEPTION_DEF[3]);
		}
		for (int i = 0; i < aJsonArray.length(); i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			JSONObject aJsoObj = null;
			try {
				aJsoObj = aJsonArray.getJSONObject(i);
				if (aJsoObj == null)
					continue;
				ScheduleInfo scheduleInfo = new ScheduleInfo();
				scheduleInfo.mPlanId = isNullInJsonObject(aJsoObj,
						IConst.Http.Plan.OUT.PLAN_ID) ? "" : aJsoObj
						.getString(IConst.Http.Plan.OUT.PLAN_ID);
				scheduleInfo.mScheduleTitle = isNullInJsonObject(aJsoObj,
						IConst.Http.Plan.OUT.TITLE) ? "" : aJsoObj
						.getString(IConst.Http.Plan.OUT.TITLE);
				String fromStr = isNullInJsonObject(aJsoObj,
						IConst.Http.Plan.OUT.START_DATE) ? null : aJsoObj
						.getString(IConst.Http.Plan.OUT.START_DATE);
				scheduleInfo.mFrom = fromStr == null ? null : mSdf
						.parse(fromStr);
				Calendar c = Calendar.getInstance();
				c.setTime(scheduleInfo.mFrom);
				scheduleInfo.mFrom = mSdf
						.parse(c.get(Calendar.YEAR)
								+ ""
								+ ((c.get(Calendar.MONTH) + 1) >= 10 ? c
										.get(Calendar.MONTH) + 1 : "0"
										+ (c.get(Calendar.MONTH) + 1))
								+ ""
								+ (c.get(Calendar.DATE) >= 10 ? c.get(Calendar.DATE)
										: "0" + c.get(Calendar.DATE))+" "+"00:01");
				String toStr = isNullInJsonObject(aJsoObj,
						IConst.Http.Plan.OUT.END_DATE) ? null : aJsoObj
						.getString(IConst.Http.Plan.OUT.END_DATE);
				scheduleInfo.mTo = fromStr == null ? null : mSdf.parse(toStr);
				c.setTime(scheduleInfo.mTo);
				scheduleInfo.mTo = mSdf
						.parse(c.get(Calendar.YEAR)
								+ ""
								+ ((c.get(Calendar.MONTH) + 1) >= 10 ? c
										.get(Calendar.MONTH) + 1 : "0"
										+ (c.get(Calendar.MONTH) + 1))
								+ ""
								+ (c.get(Calendar.DATE) >= 10 ? c.get(Calendar.DATE)
										: "0" + c.get(Calendar.DATE))+" "+"23:59");
				
				rtnList.add(scheduleInfo);
			} catch (JSONException e) {
				e.printStackTrace();
				throw new OAException(OAException.OA_EXECEPTION_DEF[2]);
			}
		}
		return rtnList;
	}
	
	public List<HashMap<String, Object>> receiveDetails(HttpClientService aService,String planId) throws Exception {
		List<HashMap<String, Object>> rtnList = new ArrayList<HashMap<String, Object>>();
		JSONArray aJsonArray = aService.getPlanDetails(IConst.Http.Plan.Detail.URL,planId);
		if(aJsonArray==null || aJsonArray.length()<=0){throw new OAException(OAException.OA_EXECEPTION_DEF[3]);}
		for(int i = 0;i<aJsonArray.length();i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			JSONObject aJsoObj = null;
			try {
				aJsoObj = aJsonArray.getJSONObject(i);
				if(aJsoObj == null)continue;
				map.put(IConst.Http.Plan.Detail.OUT.CONTENT,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.CONTENT)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.CONTENT));
				map.put(IConst.Http.Plan.Detail.OUT.CREATE_TIME,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.CREATE_TIME)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.CREATE_TIME));
				map.put(IConst.Http.Plan.Detail.OUT.CREATER_CORP_ORGAN_NAME,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.CREATER_CORP_ORGAN_NAME)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.CREATER_CORP_ORGAN_NAME));
				map.put(IConst.Http.Plan.Detail.OUT.CREATER_DEPT_ORGAN_NAME,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.CREATER_DEPT_ORGAN_NAME)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.CREATER_DEPT_ORGAN_NAME));
				map.put(IConst.Http.Plan.Detail.OUT.CREATER_NAME,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.CREATER_NAME)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.CREATER_NAME));
				map.put(IConst.Http.Plan.Detail.OUT.END_DATE,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.END_DATE)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.END_DATE));
				map.put(IConst.Http.Plan.Detail.OUT.LAST_MODIFY_NAME,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.LAST_MODIFY_NAME)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.LAST_MODIFY_NAME));
				map.put(IConst.Http.Plan.Detail.OUT.LAST_MODIFY_TIME,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.LAST_MODIFY_TIME)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.LAST_MODIFY_TIME));
				map.put(IConst.Http.Plan.Detail.OUT.PLAN_MEMBERS_NAME,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.PLAN_MEMBERS_NAME)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.PLAN_MEMBERS_NAME));
				map.put(IConst.Http.Plan.Detail.OUT.START_DATE,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.START_DATE)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.START_DATE));
				map.put(IConst.Http.Plan.Detail.OUT.TITLE,isNullInJsonObject(aJsoObj,IConst.Http.Plan.Detail.OUT.TITLE)?"":aJsoObj.getString(IConst.Http.Plan.Detail.OUT.TITLE));
				rtnList.add(map);
			} catch (JSONException e) {
				e.printStackTrace();
				throw new OAException(OAException.OA_EXECEPTION_DEF[2]);
			}
		}
		return rtnList;
	}
}
